;
var _pageUrl = "/htmlWeb/adv/list";
var pageGrid;
function addAdv() {
    if (page_Obj.page_type == 2) {
        add_Obj_view = {
            page_type: 2,//列表类型，1 广告类，2广告栏目，3广告
            pId: page_Obj.pId,//上级ID
            pId1: page_Obj.pId1,//上级ID
            opType:1,
            province: '',//省
            city: '',//市
            district: ''//区县
        };
    } else if (page_Obj.page_type == 3) {
        add_Obj_view = {
            page_type: 3,//列表类型，1 广告类，2广告栏目，3广告
            pId: page_Obj.pId,//上级ID
            pId1: page_Obj.pId1,//上级ID
            opType:1,
            province: '',//省
            city: '',//市
            district: ''//区县
        };
    }
    $.ajaxSetup({cache: false});
    $(".mainDiv").load('htmlWeb/systemAddAdverHtml', '', function () {
        add_Obj = add_Obj_view;
        $.getScript("../../by-webhtml/assets/js/system/system_addAdver.js");
    });
}
function edit(id) {
    var add_Obj_view = null;
    if (page_Obj.page_type == 2) {
        add_Obj_view = {
            page_type: 2,//列表类型，1 广告类，2广告栏目，3广告
            id: id,//编辑数据的ID
            pId: page_Obj.pId,//上级ID
            pId1: page_Obj.pId1,//上级ID
            opType:2,
            province: '',//省
            city: '',//市
            district: ''//区县
        };
    } else if (page_Obj.page_type == 3) {
        add_Obj_view = {
            page_type: 3,//列表类型，1 广告类，2广告栏目，3广告
            id: id,//编辑数据的ID
            pId: page_Obj.pId,//上级ID
            pId1: page_Obj.pId1,//上级ID
            opType:2,
            province: '',//省
            city: '',//市
            district: ''//区县
        };
    }
    $.ajaxSetup({cache: false});
    $(".mainDiv").load('htmlWeb/systemAddAdverHtml', '', function () {
        add_Obj = add_Obj_view;
        $.getScript("../../by-webhtml/assets/js/system/system_addAdver.js");
    });
}
function goView(id) {
    var page_Obj_view = null;
    if (page_Obj.page_type == 1) {
        page_Obj_view = {
            page_type: 2,//列表类型，1 广告类，2广告栏目，3广告
            id: null,//编辑数据的ID
            pId: id,//上级ID
            pId1: 0,
            province: '',//省
            city: '',//市
            district: ''//区县
        };
    } else if (page_Obj.page_type == 2) {
        page_Obj_view = {
            page_type: 3,//列表类型，1 广告类，2广告栏目，3广告
            id: null,//编辑数据的ID
            pId: id,//上级ID
            pId1: page_Obj.pId,
            province: '',//省
            city: '',//市
            district: ''//区县
        };
    }
    $.ajaxSetup({cache: false});
    $(".mainDiv").load('htmlWeb/systemAdverListHtml', '', function () {
        page_Obj = page_Obj_view;
        $.getScript("../../by-webhtml/assets/js/system/system_adverList.js");
    });
}
function init() {
    _init_area();
    if (page_Obj.page_type == 1) {
        $("#listTite").html("广告位列表");
        $("#add").hide();
        $("#page_back").hide();
        $(".searchDiv").hide();
    } else if (page_Obj.page_type == 2) {
        if (loginUserType == 5) {
            $('#province').css('display','none');
            $('#district').css('display','none');
            $("#province").val(null);
            $("#province").trigger("change");
            // $("#city").val(loginCity);
            $("#city").trigger("change");
            $("#district").val(null);
        }
        $("#listTite").html("广告栏目列表");
        $("#add").show();
        $("#page_back").show();
        $("#add").html("新建广告栏目");
        $(".searchDiv").show();
    } else if (page_Obj.page_type == 3) {
        $("#listTite").html("广告列表");
        $("#add").show();
        $("#page_back").show();
        $("#add").html("新建广告");
        $(".searchDiv").show();
        if (loginUserType == 5) {
            $('#province').css('display','none');
            $('#district').css('display','none');
            $("#province").val(null);
            $("#province").trigger("change");
            // $("#city").val(loginCity);
            $("#city").trigger("change");
            $("#district").val(null);
        }
    }
    $("#add").click(function () {
        addAdv();
    });
    $("#page_back").click(function () {
        var _p_obj = null;
        if (page_Obj.page_type == 2) {
            _p_obj = {
                page_type: 1,//列表类型，1 广告类，2广告栏目，3广告
                id: null,//编辑数据的ID
                pId: 0,//上级ID
                pId1: page_Obj.pId1,
                province: '',//省
                city: '',//市
                district: ''//区县
            };
        } else if (page_Obj.page_type == 3) {
            _p_obj = {
                page_type: 2,//列表类型，1 广告类，2广告栏目，3广告
                id: null,//编辑数据的ID
                pId: page_Obj.pId1,//上级ID
                province: '',//省
                city: '',//市
                district: ''//区县
            };
        }
        $.ajaxSetup({cache: false});
        $(".mainDiv").load('htmlWeb/systemAdverListHtml', '', function () {
            page_Obj = _p_obj;
            $.getScript("../../by-webhtml/assets/js/system/system_adverList.js");
        });
    });
    $(".conBut").click(function () {
        pageGrid.search({province: $("#province").val(), city: $("#city").val(), district: $("#district").val()});
    });
    if(loginUserType==5 && page_Obj.page_type != 1){
        $.post("htmlWeb/adv/getOption", {id: 0, parentId:0, advType:0, userId: loginUserId}, function (data) {
            var _byOpenCityList = "";
            if(data.byOpenCityList != undefined && data.byOpenCityList != null){
                $.each(data.byOpenCityList, function (index, obj) {
                    _byOpenCityList += '<option value="' + obj.city + '">' + obj.city + '</option>';
                });
            }
            $("#city").html(_byOpenCityList);
            pageBar();
        });
    }
    else {
        pageBar();
    }
}
function isCheckedAd(id) {
    $.ajax({
        url:"/htmlWeb/adv/setIsChecked",
        type:'POST',
        dataType:"json",
        data:{
            id: id
        },
        success:function(result){
            if(result.msg == 'success'){
                alert("审核成功");
                $(".conBut").click();
            }else {
                alert("审核失败");
            }
        }
    });
}
function getIsChecked(s) {
    if(s == -1){
        return "待审核";
    }else if(s == 1){
        return "审核通过";
    }else {
        return "审核不通过";
    }
}
/**
 *
 * @param id
 * @param type: 1二级删除，2三级删除
 */
function delAdv(id, type) {
    if(type == 2){
        del(id);
    }else if(type == 1){
        $.ajax({
            url:"htmlWeb/adv/getAdv",
            type:'POST',
            dataType:"json",
            data:{
                id:id
            },
            success:function(result){
                if(result.msg == 'success'){
                    if(result.r != null && result.r.length >0){
                        alert("该广告有下级广告，不允许删除！");
                        return;
                    }else {
                        del(id);
                    }
                }else {
                    alert("删除广告失败");
                }
            }
        });
    }
}
function del(id) {
    $.ajax({
        url:"htmlWeb/adv/delAdv",
        type:'POST',
        dataType:"json",
        data:{
            id:id
        },
        success:function(result){
            if(result.msg == 'success'){
                alert("删除广告成功");
                $(".conBut").click();
            }else {
                alert("删除广告失败");
            }
        }
    });
}
function pageBar() {
    pageGrid = $("#pager").createPage({
        url: _pageUrl,
        params: {
            parentId: page_Obj.page_type == 1 ? 0 : page_Obj.pId,
            province: $("#province").val(),
            city: $("#city").val(),
            district: $("#district").val()
        },
        ajaxBackFn: function (data) {
            var _gridHtml = '';
            $.each(data.rows, function (index, row) {
                if (page_Obj.page_type == 1) {
                    if(loginUserType == 5 && row.id <=3){
                        return true;
                    }
                    _gridHtml += '<tr>' +
                        '<td><input type="hidden" class="rowid" value=\"' + row.id + '\"><p>' + row.id + '</p></td>' +
                        '<td><p>' + row.name + '</p></td>' +
                        '<td><p></p></td>' +
                        '<td><p></p></td>' +
                        '<td><p></p></td>' +
                        '<td><p></p></td>' +
                        '<td><p>'+getIsChecked(row.isCheck) +'</p></td>' +
                        '<td><span class="userSpan"><p>';
                    _gridHtml += '<a href="javascript:goView(' + row.id + ')">查看广告栏目</a>';
                    _gridHtml += '</p></span></td></tr>';
                } else if (page_Obj.page_type == 2) {
                    _gridHtml += '<tr>' +
                        '<td><input type="hidden" class="rowid" value=\"' + row.id + '\"><p>' + row.id + '</p></td>' +
                        '<td><p>' + row.name + '</p></td>' +
                        '<td><p>' + (row.imgUrl == null ? "" : '<img src="'+_imgesByUrl + row.imgUrl + '" width="60px" height="40px"/>') + '</p></td>' +
                        '<td><p>' + row.url + '</p></td>' +
                        '<td><p>' + (row.province == null ? "" : row.province) + '</p></td>' +
                        '<td><p>' + (row.city == null ? "" : row.city) + '</p></td>' +
                        '<td><p>' + getIsChecked(row.isCheck) + '</p></td>' +
                        '<td><span class="userSpan"><p>';
                    _gridHtml += '<a href="javascript:edit(' + row.id + ')">编辑</a>';
                    _gridHtml += '<a href="javascript:goView(' + row.id + ')">查看广告</a>';
                    _gridHtml += '<a href="javascript:delAdv(' + row.id +','+1+ ')">删除</a>';
                    _gridHtml += '</p></span></td></tr>';
                } else if (page_Obj.page_type == 3) {
                    _gridHtml += '<tr>' +
                        '<td><input type="hidden" class="rowid" value=\"' + row.id + '\"><p>' + row.id + '</p></td>' +
                        '<td><p>' + row.name + '</p></td>' +
                        '<td><p>' + (row.imgUrl == null ? "" : '<img src="'+ _imgesByUrl+ row.imgUrl + '" width="60px" height="40px"/>') + '</p></td>' +
                        '<td><p>' + row.url + '</p></td>' +
                        '<td><p>' + (row.province == null ? "" : row.province) + '</p></td>' +
                        '<td><p>' + (row.city == null ? "" : row.city) + '</p></td>' +
                        '<td><p>' + getIsChecked(row.isCheck) + '</p></td>' +
                        '<td><span class="userSpan"><p>';
                    _gridHtml += '<a href="javascript:edit(' + row.id + ')">编辑</a>';
                    _gridHtml += '<a href="javascript:delAdv(' + row.id +','+2+ ')">删除</a>';
                    _gridHtml += '</p></span></td></tr>';
                }

            });
            $("#advGrid tr:not(:first)").html("");
            $('#advGrid').append(_gridHtml);
        }
    });
}
$(function () {
    init();
});